package com.ruralflow.flow.service.impl;

import java.util.List;
import com.ruralflow.common.utils.DateUtils;
import com.ruralflow.common.utils.SecurityUtils;
import com.ruralflow.common.utils.SequenceUtils;
import com.ruralflow.flow.domain.FlowApply;
import com.ruralflow.flow.domain.vo.FlowPassRespVO;
import com.ruralflow.flow.service.IFlowApplyService;
import com.ruralflow.population.domain.Population;
import com.ruralflow.population.service.IPopulationService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruralflow.flow.mapper.FlowPassMapper;
import com.ruralflow.flow.domain.FlowPass;
import com.ruralflow.flow.service.IFlowPassService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 通行证概览Service业务层处理
 * 
 * @author ruralflow
 * @date 2025-02-20
 */
@Service
public class FlowPassServiceImpl implements IFlowPassService 
{
    @Autowired
    private FlowPassMapper flowPassMapper;

    @Autowired
    private SequenceUtils sequenceUtils;

    @Autowired
    private IFlowApplyService flowApplyService;

    @Autowired
    private IPopulationService populationService;

    /**
     * 查询通行证概览
     * 
     * @param passId 通行证概览主键
     * @return 通行证概览
     */
    @Override
    public FlowPassRespVO selectFlowPassByPassId(Long passId)
    {
        FlowPassRespVO flowPassRespVO = new FlowPassRespVO();
        FlowPass flowPass = flowPassMapper.selectFlowPassByPassId(passId);
        // 设置下通行证信息
        BeanUtils.copyProperties(flowPass, flowPassRespVO);
        // 查找人员信息
        // 1.查找申请信息（根据申请id）
        FlowApply flowApply = flowApplyService.selectFlowApplyByApplyId(flowPass.getApplyId());
        // 2.根据申请信息中的申请人id查找人员信息
        Population population = populationService.selectPopulationByPopulationId(flowApply.getPopulationId());
        flowPassRespVO.setPopulation(population);
        return flowPassRespVO;
    }

    /**
     * 查询通行证概览列表
     * 
     * @param flowPass 通行证概览
     * @return 通行证概览
     */
    @Override
    public List<FlowPass> selectFlowPassList(FlowPass flowPass)
    {
        return flowPassMapper.selectFlowPassList(flowPass);
    }

    /**
     * 新增通行证概览
     * 
     * @param flowPass 通行证概览
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertFlowPass(FlowPass flowPass)
    {
        flowPass.setCreateTime(DateUtils.getNowDate());
        flowPass.setCreateBy(SecurityUtils.getUsername());
        flowPass.setPassCode(getNo());
        return flowPassMapper.insertFlowPass(flowPass);
    }

    /**
     * 修改通行证概览
     * 
     * @param flowPass 通行证概览
     * @return 结果
     */
    @Override
    public int updateFlowPass(FlowPass flowPass)
    {
        flowPass.setUpdateTime(DateUtils.getNowDate());
        flowPass.setUpdateBy(SecurityUtils.getUsername());
        return flowPassMapper.updateFlowPass(flowPass);
    }

    /**
     * 批量删除通行证概览
     * 
     * @param passIds 需要删除的通行证概览主键
     * @return 结果
     */
    @Override
    public int deleteFlowPassByPassIds(Long[] passIds)
    {
        String updateBy = SecurityUtils.getUsername();
        return flowPassMapper.deleteFlowPassByPassIds(passIds, updateBy);
    }

    /**
     * 删除通行证概览信息
     * 
     * @param passId 通行证概览主键
     * @return 结果
     */
    @Override
    public int deleteFlowPassByPassId(Long passId)
    {
        String updateBy = SecurityUtils.getUsername();
        return flowPassMapper.deleteFlowPassByPassId(passId, updateBy);
    }

    @Override
    public Integer selectCountFlowPass() {
        return flowPassMapper.selectCountFlowPass();
    }

    /**
     * 生成通行证编码
     *
     * @return 通行证编码
     */
    private String getNo() {
        // 获取用户id补0到五位
        Long userId = SecurityUtils.getUserId();
        String userIdStr = String.format("%05d", userId); // 补零到5位
        return sequenceUtils.createSequenceNumber("FLP" + userIdStr);
    }
}
